package com.onemile.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import com.onemile.entity.Session;

import android.util.Log;

public class UtiNet
{
   public static boolean HttpPostData(String url, String postData, PostCallBack callBack)
   {
	  DefaultHttpClient client = new DefaultHttpClient();
	  HttpPost post = new HttpPost(url);
	  StringEntity se=null;
	  try
	  {
		 se = new StringEntity(postData,HTTP.UTF_8);
	     post.setEntity(se); 
		 HttpResponse response = client.execute(post);
		 if (response.getStatusLine().getStatusCode() == 200)// 成功
		 {
			callBack.DoWhenPostComplete(response);
//			InputStream is = response.getEntity().getContent();
//			XmlPullParser parser = Xml.newPullParser();

//			try
//			{
//			   parser.setInput(is, "UTF-8");
//			   int eventType = parser.getEventType();
//
//			   while (eventType != XmlPullParser.END_DOCUMENT)
//			   {
//				  switch (eventType)
//				  {
//					 case XmlPullParser.START_TAG:// 开始元素事件
//
//						if (parser.getName().equalsIgnoreCase("return"))
//						{
//						   if (parser.nextText().equals("T"))
//						   {
//							  return true;
//						   }
//						   else
//						   {
//							  return false;
//						   }
//						}
//						break;
//				  }
//				  eventType = parser.next();
//			   }
//			   return false;
//			}
//			catch (XmlPullParserException e)
//			{
//			   if (Session.getInstance().IsDEBUG())
//			   {
//				  Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
//			   }
//			}
		 }
		 else
		 {
			Log.e("ERROR", "UtiNet_HttpPostData()--" + response.getStatusLine().toString());
		 }
	  }
	  catch (UnsupportedEncodingException e1)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e1);
		 }
	  }
	  catch (ClientProtocolException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
		 }
	  }
	  catch (IOException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
		 }
	  }
	  return false;
   }
   
   public interface PostCallBack
   {
	  public void DoWhenPostComplete(HttpResponse response);
   }
}
